package com.example.stock.ui.main;

import android.content.DialogInterface;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;

import com.example.stock.R;
import com.example.stock.base.BaseActivity;
import com.example.stock.base.CommonCallback;
import com.example.stock.base.Result;
import com.example.stock.config.Config;
import com.example.stock.databinding.ActivityMainBinding;
import com.example.stock.model.MainItemBean;
import com.example.stock.ui.login.LoginActivity;
import com.example.stock.ui.order.GoodsActivity;
import com.example.stock.util.DialogUtils;
import com.example.stock.util.SharedPreferencesUtils;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {

    private final int[] ICONS = {
            R.drawable.ic_download,
            R.drawable.ic_camera,
            R.drawable.ic_folder,
            R.drawable.ic_upload,
            R.drawable.ic_delete,
            R.drawable.ic_setting,
    };

    private final String[] NAMES = {
            "同步库单:download",
            "库单扫码:modify",
            "本地查询:query",
            "上传库单:upload",
            "清空数据:clear",
            "系统设置:settings"
    };

    @Override
    protected Class<MainViewModel> initViewModel() {
        return MainViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        int space = getResources().getDimensionPixelSize(R.dimen.layout_margin_10dp);
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mBinding.recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                //由于每行都只有2个，所以第一个都是2的倍数，把左边距设为0
                if (parent.getChildLayoutPosition(view) % 2 == 0) {
                    outRect.right = space;
                    outRect.left = 0;
                } else {
                    outRect.right = 0;
                    outRect.left = space;
                }
            }
        });
        MainItemAdapter itemAdapter = new MainItemAdapter(getMainBeans());
        mBinding.recyclerView.setAdapter(itemAdapter);
        itemAdapter.setListener(new CommonCallback<Object>() {
            @Override
            public void onResult(Object... data) {
                MainItemBean itemBean = (MainItemBean) data[0];
                boolean downloadFinish = SharedPreferencesUtils.isDownloadFinish();
                switch (itemBean.type) {
                    case "download":
                        if (downloadFinish) {
                            showDialogInfo("本地有未提交的数据，请先删除本地数据");
                        } else {
                            mViewModel.getArriveOrder();
                        }
                        break;
                    case "modify":
                        if (!downloadFinish) {
                            showDialogInfo("请更新到货单");
                        } else {
                            GoodsActivity.start(MainActivity.this, Config.GOOD_TYPE_SCAN);
                        }
                        break;
                    case "query":
                        if (!downloadFinish) {
                            showDialogInfo("请更新到货单");
                        } else {
                            GoodsActivity.start(MainActivity.this, Config.GOOD_TYPE_QUERY);
                        }
                        break;
                    case "upload":
                        if (!downloadFinish) {
                            showDialogInfo("请更新到货单");
                        } else {
                            GoodsActivity.start(MainActivity.this, Config.GOOD_TYPE_UPLOAD);
                        }
                        break;
                    case "clear":
                        if (!downloadFinish) {
                            showDialogInfo("请更新到货单");
                        } else {
                            DialogUtils.showInfoDialog(MainActivity.this, "是否清除本地数据？", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    DialogUtils.closeInfoDialog();
                                    mViewModel.clearDB();
                                }
                            }, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    DialogUtils.closeInfoDialog();
                                }
                            });
                        }
                        break;
                    case "settings":
                        LoginActivity.start(MainActivity.this);
                        break;
                    default:
                        break;
                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        String ip = SharedPreferencesUtils.getIP();
        String port = SharedPreferencesUtils.getPort();
        if (!TextUtils.isEmpty(ip) && TextUtils.isEmpty(port)) {
            LoginActivity.start(MainActivity.this);
        }
    }

    public List<MainItemBean> getMainBeans() {
        List<MainItemBean> itemBeans = new ArrayList<>();
        int len = ICONS.length;
        for (int i = 0; i < len; i++) {
            String[] split = NAMES[i].split(":");
            MainItemBean itemBean = new MainItemBean(ICONS[i], split[0], split[1]);
            itemBeans.add(itemBean);

        }
        return itemBeans;
    }

    @Override
    protected void registerObserve() {
        super.registerObserve();
        mViewModel.clearResult.observe(this, new Observer<Result<Object>>() {
            @Override
            public void onChanged(Result<Object> objectResult) {
                Snackbar.make(mBinding.recyclerView, objectResult.getMsg(), Snackbar.LENGTH_LONG).setDuration(2000).show();
                if (objectResult.isSuccess()) {
                    SharedPreferencesUtils.setDownloadFinish(false);
                }
            }
        });
        mViewModel.hasResult.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    showDialogInfo("同步货单成功");
                }
            }
        });
    }

    private void showDialogInfo(String str) {
        DialogUtils.showInfoDialog(MainActivity.this, str, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DialogUtils.closeInfoDialog();
            }
        });
    }
}
